using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ASPNETVN.PORTAL.Modules.ACTP.Components
{
	public partial class Employee
	{
		#region Properties.
		
		public int EmployeeID { set; get; }
		public Guid ModuleID { set; get; }
		public int MembershipID { set; get; }
		public string EmployeeCode { set; get; }
		public string FirstName { set; get; }
		public string LastName { set; get; }
		public string PersonalTitle { set; get; }
		public string JobTitle { set; get; }
		public bool Gender { set; get; }
		public string CreatedBy { set; get; }
		public DateTime CreatedDate { set; get; }
		public string UpdatedBy { set; get; }
		public DateTime UpdatedDate { set; get; }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Methods
		protected static IList<Employee> ConvertToCollection(IDataReader reader)
		{
			IList<Employee> collection = new List<Employee>();
			while (reader.Read())
			{
				Employee entity = new Employee();
				if (!reader.IsDBNull(reader.GetOrdinal("EmployeeID"))) entity.EmployeeID = reader.GetInt32(reader.GetOrdinal("EmployeeID"));
				if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
				if (!reader.IsDBNull(reader.GetOrdinal("MembershipID"))) entity.MembershipID = reader.GetInt32(reader.GetOrdinal("MembershipID"));
				if (!reader.IsDBNull(reader.GetOrdinal("EmployeeCode"))) entity.EmployeeCode = reader.GetString(reader.GetOrdinal("EmployeeCode"));
				if (!reader.IsDBNull(reader.GetOrdinal("FirstName"))) entity.FirstName = reader.GetString(reader.GetOrdinal("FirstName"));
				if (!reader.IsDBNull(reader.GetOrdinal("LastName"))) entity.LastName = reader.GetString(reader.GetOrdinal("LastName"));
				if (!reader.IsDBNull(reader.GetOrdinal("PersonalTitle"))) entity.PersonalTitle = reader.GetString(reader.GetOrdinal("PersonalTitle"));
				if (!reader.IsDBNull(reader.GetOrdinal("JobTitle"))) entity.JobTitle = reader.GetString(reader.GetOrdinal("JobTitle"));
				if (!reader.IsDBNull(reader.GetOrdinal("Gender"))) entity.Gender = reader.GetBoolean(reader.GetOrdinal("Gender"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("UpdatedBy"))) entity.UpdatedBy = reader.GetString(reader.GetOrdinal("UpdatedBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("UpdatedDate"))) entity.UpdatedDate = reader.GetDateTime(reader.GetOrdinal("UpdatedDate"));
				collection.Add(entity);
			}
			reader.Close();
			return collection;
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static Employee Load(int employeeID)
		{
			const string spName = "[dbo].[p_ACTP_Employee_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@EmployeeID", SqlDbType.Int, employeeID);
            IDataReader reader = db.ExecuteReader(dbCommand);
			IList<Employee> collection = ConvertToCollection(reader);	
			if (collection.Count > 0)
			{
				return collection[0];
			}
			return null;
		}		
		
		//---------------------------------------------------------------------------------------------
		public static IList<Employee> SelectCollectionAll(Guid moduleID)
		{
			IDataReader reader = SelectReaderAll(moduleID);
			return ConvertToCollection(reader);			
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static IList<Employee> SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			IDataReader reader = SelectReaderDynamic(whereCondition, orderByExpression);
			return ConvertToCollection(reader);		
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		public static IList<Employee> SelectCollectionBy_MembershipID(int membershipID)
		{
            IDataReader reader = SelectReaderBy_MembershipID(membershipID);
			return ConvertToCollection(reader);	
		}		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectBy_MembershipID(int membershipID)
		{
			const string spName = "[dbo].[p_ACTP_Employee_SelectBy_MembershipID]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@MembershipID", SqlDbType.Int, membershipID);
						
            return db.ExecuteDataSet(dbCommand);
		}
		//---------------------------------------------------------------------------------------------

		public static DataSet SelectAll(Guid moduleID)
        {
            const string spName = "[dbo].[p_ACTP_Employee_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_ACTP_Employee_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll(Guid moduleID)
        {
            const string spName = "[dbo].[p_ACTP_Employee_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_ACTP_Employee_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		public static IDataReader SelectReaderBy_MembershipID(int membershipID)
		{
			const string spName = "p_ACTP_Employee_SelectBy_MembershipID";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@MembershipID", SqlDbType.Int, membershipID);
			
            return db.ExecuteReader(dbCommand);
		}		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertEmployee(Guid moduleID, int membershipID, string employeeCode, string firstName, string lastName, string personalTitle, string jobTitle, bool gender, string createdBy, DateTime createdDate, string updatedBy, DateTime updatedDate)
		{
			Employee entity = new Employee();	
			entity.ModuleID = moduleID;
			entity.MembershipID = membershipID;
			entity.EmployeeCode = employeeCode;
			entity.FirstName = firstName;
			entity.LastName = lastName;
			entity.PersonalTitle = personalTitle;
			entity.JobTitle = jobTitle;
			entity.Gender = gender;
			entity.CreatedBy = createdBy;
			entity.CreatedDate = createdDate;
			entity.UpdatedBy = updatedBy;
			entity.UpdatedDate = updatedDate;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_ACTP_Employee_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddOutParameter(dbCommand, "@EmployeeID", SqlDbType.Int, 4);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@MembershipID", SqlDbType.Int, MembershipID);
			db.AddInParameter(dbCommand, "@EmployeeCode", SqlDbType.VarChar, EmployeeCode);
			db.AddInParameter(dbCommand, "@FirstName", SqlDbType.NVarChar, FirstName);
			db.AddInParameter(dbCommand, "@LastName", SqlDbType.NVarChar, LastName);
			db.AddInParameter(dbCommand, "@PersonalTitle", SqlDbType.NVarChar, PersonalTitle);
			db.AddInParameter(dbCommand, "@JobTitle", SqlDbType.NVarChar, JobTitle);
			db.AddInParameter(dbCommand, "@Gender", SqlDbType.Bit, Gender);
			db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);
			db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year <= 1753 ? DBNull.Value : (object) CreatedDate);
			db.AddInParameter(dbCommand, "@UpdatedBy", SqlDbType.VarChar, UpdatedBy);
			db.AddInParameter(dbCommand, "@UpdatedDate", SqlDbType.DateTime, UpdatedDate.Year <= 1753 ? DBNull.Value : (object) UpdatedDate);
			
			if (transaction != null)
			{
				db.ExecuteNonQuery(dbCommand, transaction);
				EmployeeID = (int) db.GetParameterValue(dbCommand, "@EmployeeID");
				return EmployeeID;
			}
            else
			{
				db.ExecuteNonQuery(dbCommand);
				EmployeeID = (int) db.GetParameterValue(dbCommand, "@EmployeeID");
				return EmployeeID;
			}			
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool InsertCollection(IList<Employee> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using(SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Employee item in collection)
						{
							if (item.Insert(transaction) <= 0)
							{							
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateEmployee(int employeeID, Guid moduleID, int membershipID, string employeeCode, string firstName, string lastName, string personalTitle, string jobTitle, bool gender, string createdBy, DateTime createdDate, string updatedBy, DateTime updatedDate)
		{
			Employee entity = new Employee();			
			entity.EmployeeID = employeeID;
			entity.ModuleID = moduleID;
			entity.MembershipID = membershipID;
			entity.EmployeeCode = employeeCode;
			entity.FirstName = firstName;
			entity.LastName = lastName;
			entity.PersonalTitle = personalTitle;
			entity.JobTitle = jobTitle;
			entity.Gender = gender;
			entity.CreatedBy = createdBy;
			entity.CreatedDate = createdDate;
			entity.UpdatedBy = updatedBy;
			entity.UpdatedDate = updatedDate;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_ACTP_Employee_InsertUpdate";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@EmployeeID", SqlDbType.Int, EmployeeID);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@MembershipID", SqlDbType.Int, MembershipID);
			db.AddInParameter(dbCommand, "@EmployeeCode", SqlDbType.VarChar, EmployeeCode);
			db.AddInParameter(dbCommand, "@FirstName", SqlDbType.NVarChar, FirstName);
			db.AddInParameter(dbCommand, "@LastName", SqlDbType.NVarChar, LastName);
			db.AddInParameter(dbCommand, "@PersonalTitle", SqlDbType.NVarChar, PersonalTitle);
			db.AddInParameter(dbCommand, "@JobTitle", SqlDbType.NVarChar, JobTitle);
			db.AddInParameter(dbCommand, "@Gender", SqlDbType.Bit, Gender);
			db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);
			db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year <= 1753 ? DBNull.Value : (object) CreatedDate);
			db.AddInParameter(dbCommand, "@UpdatedBy", SqlDbType.VarChar, UpdatedBy);
			db.AddInParameter(dbCommand, "@UpdatedDate", SqlDbType.DateTime, UpdatedDate.Year <= 1753 ? DBNull.Value : (object) UpdatedDate);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		public static bool InsertUpdateCollection(IList<Employee> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Employee item in collection)
						{
							if (item.InsertUpdate(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);
						
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}	
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateEmployee(int employeeID, Guid moduleID, int membershipID, string employeeCode, string firstName, string lastName, string personalTitle, string jobTitle, bool gender, string createdBy, DateTime createdDate, string updatedBy, DateTime updatedDate)
		{
			Employee entity = new Employee();			
			entity.EmployeeID = employeeID;
			entity.ModuleID = moduleID;
			entity.MembershipID = membershipID;
			entity.EmployeeCode = employeeCode;
			entity.FirstName = firstName;
			entity.LastName = lastName;
			entity.PersonalTitle = personalTitle;
			entity.JobTitle = jobTitle;
			entity.Gender = gender;
			entity.CreatedBy = createdBy;
			entity.CreatedDate = createdDate;
			entity.UpdatedBy = updatedBy;
			entity.UpdatedDate = updatedDate;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_ACTP_Employee_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@EmployeeID", SqlDbType.Int, EmployeeID);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@MembershipID", SqlDbType.Int, MembershipID);
			db.AddInParameter(dbCommand, "@EmployeeCode", SqlDbType.VarChar, EmployeeCode);
			db.AddInParameter(dbCommand, "@FirstName", SqlDbType.NVarChar, FirstName);
			db.AddInParameter(dbCommand, "@LastName", SqlDbType.NVarChar, LastName);
			db.AddInParameter(dbCommand, "@PersonalTitle", SqlDbType.NVarChar, PersonalTitle);
			db.AddInParameter(dbCommand, "@JobTitle", SqlDbType.NVarChar, JobTitle);
			db.AddInParameter(dbCommand, "@Gender", SqlDbType.Bit, Gender);
			db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);
			db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year <= 1753 ? DBNull.Value : (object) CreatedDate);
			db.AddInParameter(dbCommand, "@UpdatedBy", SqlDbType.VarChar, UpdatedBy);
			db.AddInParameter(dbCommand, "@UpdatedDate", SqlDbType.DateTime, UpdatedDate.Year <= 1753 ? DBNull.Value : (object) UpdatedDate);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool UpdateCollection(IList<Employee> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Employee item in collection)
						{
							if (item.Update(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at UpdateCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteEmployee(int employeeID)
		{
			Employee entity = new Employee();
			entity.EmployeeID = employeeID;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_ACTP_Employee_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@EmployeeID", SqlDbType.Int, EmployeeID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static int DeleteBy_MembershipID(int membershipID)
		{
			const string spName = "[dbo].[p_ACTP_Employee_DeleteBy_MembershipID]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@MembershipID", SqlDbType.Int, membershipID);
						
            return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
			
		
		public static int DeleteDynamic(string whereCondition)
		{
			const string spName = "[dbo].[p_ACTP_Employee_DeleteDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            
            return db.ExecuteNonQuery(dbCommand);   
		}
		//---------------------------------------------------------------------------------------------
		
		public static bool DeleteCollection(IList<Employee> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Employee item in collection)
						{
							if (item.Delete(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at DeleteCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		#endregion
	}	
}